package asapIs134CK.womi.client.profile;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SignInUserDisplay extends Composite{
	private UserDisplay _parent;
	private VerticalPanel panel;
	private Label nickName;
	private TextBox txtNickName;
	private Label email;
	private TextBox txtEmail;
	private Label pwd1;
	private TextBox txtPwd1;
	private Label pwd2;
	private TextBox txtPwd2;
	private Button signIn;
	private Button cancel;
	private boolean validEmail;
	private boolean validPwd;
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final RPCUserServiceAsync userService = GWT
			.create(RPCUserService.class);
	
	public SignInUserDisplay(UserDisplay parent){
		_parent = parent;
		validEmail = false;
		validPwd = false;
		panel = new VerticalPanel();
		nickName = new Label("Nickname :");
		txtNickName = new TextBox();
		email = new Label("Email");
		txtEmail = new TextBox();
		ChangeHandler emailChangeHandler = new ChangeHandler(){

			public void onChange(ChangeEvent event) {
				emailValidator();
			}
			
		};
		txtEmail.addChangeHandler(emailChangeHandler );
		pwd1 = new Label("Password :");
		txtPwd1 = new PasswordTextBox();
		pwd2 = new Label("Re-enter password :");
		txtPwd2 = new PasswordTextBox();
		ChangeHandler pwd2Changehandler = new ChangeHandler(){

			public void onChange(ChangeEvent event) {
				pwdValidator();
			}
		};
		txtPwd2.addChangeHandler(pwd2Changehandler );
		ClickHandler signinClickHandler = new ClickHandler(){

			public void onClick(ClickEvent event) {
				addUser();
			}
			
		};
		signIn = new Button("Sign in", signinClickHandler );
		signIn.setEnabled(false);
		ClickHandler cancelClickHandler = new ClickHandler(){

			public void onClick(ClickEvent event) {
				_parent.displayDefault();
			}
			
		};
		cancel = new Button("Cancel", cancelClickHandler );
		panel.add(nickName);
		panel.add(txtNickName);
		panel.add(email);
		panel.add(txtEmail);
		panel.add(pwd1);
		panel.add(txtPwd1);
		panel.add(pwd2);
		panel.add(txtPwd2);
		FlowPanel buttons = new FlowPanel();
		buttons.add(signIn);
		buttons.add(cancel);
		panel.add(buttons);
		
		initWidget(panel);
	}

	private void emailValidator() {
		String strEmail = txtEmail.getText();
		if(isValidEmailAddress(strEmail)){
			validEmail = true;
		}
		else {
			validEmail = false;
			Window.alert("Please enter a valid email address");
		}
		validateForm();
	}


	private void pwdValidator() {
		if(txtPwd1.getText() != "" && txtPwd1.getText().equals(txtPwd2.getText())){
			validPwd = true;
		}
		else {
			validPwd = false;
			Window.alert("Passwords do not match.");
		}
		validateForm();
	}

	private void addUser() {
		String strNickName = txtNickName.getText();
		String strEmail = txtEmail.getText();
		//TODO : gérer les avatars
		String strAvatar = "";
		// NOTE : le mdp est encrypté coté server. Il existe des méthodes (sales !) 
		// pour le faire coté client cependant.
		String strPwd = txtPwd1.getText();
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
				Window.alert("The server could not be reached, please check your network connection.");
				_parent.displayDefault();
			}

			public void onSuccess(Boolean result) {
				// TODO : change name of this method
				_parent.displayDefault();
			}
		};
		UserClient newUser = new UserClient(strNickName, strEmail, strAvatar, strPwd);
		panel.clear();
		panel.add(_parent.getWait());
		userService.addUser(newUser, callback);
	}
	private boolean isValidEmailAddress(String strEmail){ 
		boolean res = false;
         if (strEmail != null && strEmail.matches(".+@.+\\.[a-z]+")) {
                 res = true; 
         }         
         return res; 
	}
	
	private void validateForm(){
		if(validEmail && validPwd){
			signIn.setEnabled(true);
		}
		else{
			signIn.setEnabled(false);
		}
	}
	
}
